package com.example.myapplication.view

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import com.example.myapplication.databinding.ViewGardenBoxAppearBinding
import com.example.myapplication.shortvideo.utils.DisplayUtils

/**
 * @description:
 *
 * @author: wangrongfeng
 *
 * @create: 2023-08-27 11:51
 **/
class GardenBoxAppearView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    val mBinding: ViewGardenBoxAppearBinding
    var frameLayout: FrameLayout? = null

    init {
        mBinding = ViewGardenBoxAppearBinding.inflate(LayoutInflater.from(context), this, true)
    }

    fun show(layout: FrameLayout) {
        this.frameLayout = layout
        layout.addView(this)
        mHandler.sendEmptyMessageDelayed(1, 1000)
    }

    private val mHandler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            if (msg.what == 1) {
                val scaleX: ObjectAnimator =
                    ObjectAnimator.ofFloat(this@GardenBoxAppearView, "scaleX", 1f, 0.2f)
                        .setDuration(800)
                val scaleY: ObjectAnimator =
                    ObjectAnimator.ofFloat(this@GardenBoxAppearView, "scaleY", 1f, 0.2f)
                        .setDuration(800)
                val translationX = ObjectAnimator.ofFloat(
                    this@GardenBoxAppearView,
                    "translationX",
                    0f,
                    DisplayUtils.getWidthPixels() / 2f - DisplayUtils.dp2px(30f),
                ).setDuration(800)

                val translationY = ObjectAnimator.ofFloat(
                    this@GardenBoxAppearView,
                    "translationY",
                    0f,
                    DisplayUtils.getHeightPixels() / 2 - DisplayUtils.dp2px(80f),
                ).setDuration(800)

                val animatorSet = AnimatorSet()

                animatorSet.playTogether(scaleX, scaleY, translationX, translationY)
                animatorSet.duration = 800
                animatorSet.interpolator = LinearInterpolator()
                animatorSet.addListener(object : Animator.AnimatorListener {
                    override fun onAnimationStart(p0: Animator?) {
                    }

                    override fun onAnimationEnd(p0: Animator?) {
                        sendEmptyMessageDelayed(2, 500)
                    }

                    override fun onAnimationCancel(p0: Animator?) {
                    }

                    override fun onAnimationRepeat(p0: Animator?) {

                    }
                })
                animatorSet.start()
            } else if (msg.what == 2) {
                frameLayout?.removeView(this@GardenBoxAppearView)
            }
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mHandler.removeMessages(1)
        mHandler.removeMessages(2)
    }

}